#ifndef __M_UTIL_M__
#define __M_UTIL_M__
/*实用工具类的实现:
    1. 获取系统时间
    2. 判断文件是否存在
    3. 获取文件所在路径
    4. 创建目录
*/
#include <iostream>
#include <ctime>
#include <sys/stat.h>

namespace log
{
    namespace util
    {
        class Date
        {
            public:
            static time_t now()
            {
                return time(0);
            }
        };
        class file
        {
            public:
            static bool exists(const std::string pathname) //判断文件是否存在
            {
                struct stat st;
                if(stat(pathname.c_str(),&st) <0) return false;
                return true;
            }
            static void createDirectory(const std::string pathname) //创建文件夹
            {
                size_t pos =0;
                size_t cpos =0;

                while(pos <pathname.size())
                {
                    pos = pathname.find_first_of("/\\",cpos);
                    if(pos == pathname.npos )
                    {
                        mkdir(pathname.c_str(),0777);
                        //break;
                    }

                    std::string parent_str = pathname.substr(0,pos);
                    if(exists(parent_str))
                    { 
                        cpos = pos+1;
                        continue;
                    }

                    mkdir(parent_str.c_str(),0777);
                    cpos = pos+1;
                }
            }
            static std::string path(const std::string pathname) //获取当前目录
            {
                size_t pos = pathname.find_last_of("/\\");
                if(pos == pathname.npos) return ".";
                
                return pathname.substr(0,pos+1); 
            }
        };
    }
}

#endif